var app = getApp()
Page({

  data: {
    show: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    // selectData: ['视频', '频道'],//下拉列表的数据
    index: 0, //选择的下拉列表下标
    searchInput: '',
    searchRecord: [],
    hotSearchData: [],
    clear: false,
    inputxs: true,
  },
  onchange: function(event) {
    let that = this;
    var inputSearch = event.detail.value;
    that.setData({
      searchInput: inputSearch,
    })
    that.setData({
      clear: inputSearch ? true : false
    })
  },
  // 清空inpunt
  bindXS: function() {
    this.setData({
      searchInput: '',
      clear: false,
    })
  },

  qingkolist: function() {
    var inputxs = this.data.inputxs
    this.setData({
      searchRecord: [],
      inputxs: false,
    })
    wx.removeStorage({
      key: 'searchRecord',
      success(res) {
        console.log(res)
      }
    })
    // wx.removeStorage('searchRecord', [])
  },
  // 点击下拉显示框
  selectTap() {
    this.setData({
      show: !this.data.show
    });
  },
  // 点击下拉列表
  optionTap(e) {
    let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
    this.setData({
      index: Index,
      show: !this.data.show
    });
  },
  // getSearchInput: function (e){
  //   this.setData({
  //     searchInput:e.detail.value
  //   })
  // },

  openHistorySearch: function() {
    this.setData({
      searchRecord: wx.getStorageSync('searchRecord') || [], //若无储存则为空
    })
  },
  //点击搜索按钮提交表单跳转并储存历史记录  
  searchSubmitFn: function(e) {
    // console.log(e)    
    var that = this
    var inputxs = that.data.inputxs

    that.setData({
      inputxs: searchRecord ? false : true
    })
    var searchInput = that.data.searchInput
    searchInput = searchInput.replace(/\s+/g, '');
    var searchRecord = that.data.searchRecord
    if (searchInput == '') { //输入为空时的处理    
      app.showTipMsg('请输关键词');
      return false;
    } else {
      that.setData({
        searchInput: ''
      })
      let arrnum = searchRecord.indexOf(searchInput);
      //判断是否是重复的搜索记录
      if (arrnum == -1) {
        searchRecord.unshift(searchInput)
        //将历史记录数组整体储存到缓存中    
      } else {
        // 删除已存在后重新插入至数组
        searchRecord.splice(arrnum, 1)
        searchRecord.unshift(searchInput)

      }
      wx.setStorageSync('searchRecord', searchRecord)
      wx.navigateTo({
        url: '../../shopPages/searchView/searchView?keywords=' + searchInput,
      })
    }
    that.setData({
      searchRecord: that.data.searchRecord
    })
  },
  deleteHistory: function() {
    wx.clearStorageSync('searhRecord')
    this.setData({
      searchRecord: []
    })
  },
  turnSearch: function(e) {
    // console.log(this.data.searchRecord)
    // console.log(e)
    this.setData({
      searchInput: e.currentTarget.dataset.name
    })
    this.searchSubmitFn()
  },
  returnLastPage: function() {
    wx.navigateBack({
      delta: 1
    })
  },

  onLoad: function(options) {
    this.openHistorySearch();
    var that = this
    var url2 = "/mall/get_search_data"
    var params2 = {};
    app.HttpPostSend(url2, params2, function(res) {

      if (res.msg !== "success") {
        app.showTipMsg(res.msg);
      } else {
        that.setData({
          hotSearchData: res.hot_search
        });
      }
    });
    var searchRecord = wx.getStorageSync('searchRecord')
    var inputxs = that.data.inputxs
    that.setData({
      inputxs: searchRecord ? true : false
    })
  },
  onShow: function() {
    this.openHistorySearch();
    var searchRecord = wx.getStorageSync('searchRecord');
    var that = this;
    var inputxs = that.data.inputxs;
    that.setData({
      inputxs: searchRecord ? true : false
    })
  },
  toGoodsList: function(e) {
    var that = this;
    var goods_name = e.currentTarget.dataset.goods_name;
    var searchInput = e.currentTarget.dataset.name
    console.log(searchInput)
    var searchRecord = that.data.searchRecord
    let arrnum = searchRecord.indexOf(searchInput);
    //判断是否是重复的搜索记录
    if (arrnum == -1) {
      searchRecord.unshift(searchInput)
      //将历史记录数组整体储存到缓存中    
    } else {
      // 删除已存在后重新插入至数组
      searchRecord.splice(arrnum, 1)
      searchRecord.unshift(searchInput)
    }
    wx.setStorageSync('searchRecord', searchRecord)
    wx.navigateTo({
      url: '../../shopPages/searchView/searchView?keywords=' + goods_name,
    })
  }
})